﻿using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ldhWebApiWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public IDisposable SignalR { get; set; }
        int httpPort = 8089;
        int readTimiePort = 8080;
        int debug = 1;
        public MainWindow()
        {
            InitializeComponent();
            App.Current.Exit += Current_Exit;
            Closing += MainWindow_Closing;
            Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Loaded -= MainWindow_Loaded;
            if (!int.TryParse(ConfigurationManager.AppSettings["http"], out httpPort))
            {
                MessageBox.Show("http端口读取错误，请检查config文件的端口配置。");
                return;
            }
            if (!int.TryParse(ConfigurationManager.AppSettings["realtime"], out readTimiePort))
            {
                MessageBox.Show("RealTime端口读取错误，请检查config文件的端口配置。");
                return;
            }
            if (!int.TryParse(ConfigurationManager.AppSettings["debug"], out debug))
            {
                MessageBox.Show("Debug标识读取错误，请检查config文件的端口配置。");
                return;
            }
            if (debug == 1)
            {
                string baseAddress = "http://localhost:" + httpPort.ToString() + "/";
                Task.Run(() =>
                {
                    WebApp.Start<WebApi>(url: baseAddress);
                    this.Dispatcher.Invoke(() =>
                    {
                        lb_msg.Items.Insert(0, "WebApi Server is run in " + baseAddress);
                    });
                });
                string ServerURI = "http://localhost:" + readTimiePort.ToString() + "/";
                Task.Run(() =>
                {
                    SignalR = WebApp.Start<Startup_Signalr>(url: ServerURI);
                    this.Dispatcher.Invoke(() =>
                    {
                        lb_msg.Items.Insert(0, "Signalr Server is run in " + ServerURI);
                    });
                });
            }
            else
            {
                Task.Run(() =>
                {
                    string baseAddress = "http://*:" + httpPort.ToString();
                    var options = new StartOptions(baseAddress)
                    {
                        ServerFactory = "Microsoft.Owin.Host.HttpListener"
                    };
                    WebApp.Start<WebApi>(options);
                    this.Dispatcher.Invoke(() =>
                    {
                        lb_msg.Items.Insert(0, "WebApi Server is run in " + baseAddress);
                    });
                });

                Task.Run(() =>
                {
                    string ServerURI = "http://*:" + readTimiePort.ToString();
                    var options = new StartOptions(ServerURI)
                    {
                        ServerFactory = "Microsoft.Owin.Host.HttpListener"
                    };
                    SignalR = WebApp.Start<Startup_Signalr>(options);
                    this.Dispatcher.Invoke(() =>
                    {
                        lb_msg.Items.Insert(0, "Signalr Server is run in " + ServerURI);
                    });
                });
            }
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            Application.Current.MainWindow.Visibility = Visibility.Hidden;
        }

        void Current_Exit(object sender, ExitEventArgs e)
        {
            SignalR.Dispose();
        }

    }
}
